//
//  AccountDepositTableViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/11.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "AccountDepositViewController.h"
#import "AccountDepositNextViewController.h"
#import "SheetView.h"
#import "HYNetHelper.h"
#import "JFAreaCodeTableViewController.h"

@interface AccountDepositViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *SMSTextField;

@end

@implementation AccountDepositViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = [NSString stringWithFormat:@"%@ %@", @[@"Deposit", @"Exchange", @"Transfer", @"Withdraw"][self.type], self.balance.asset_symbol];
    
    [self.nextButton setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(0xCCCCCC) size:self.nextButton.frame.size] forState:UIControlStateDisabled];
    [self.nextButton setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(0x70896A) size:self.nextButton.frame.size] forState:UIControlStateNormal];
    self.nextButton.enabled = NO;
    
    [self.codeTextField addTarget:self action:@selector(onValueDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.phoneTextField addTarget:self action:@selector(onValueDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.SMSTextField addTarget:self action:@selector(onValueDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.SMSTextField addSendButton];
    WEAKSELF
    self.SMSTextField.block = ^{
        [weakSelf senderSMS];
    };
    
    [self requsetUsers];
}

- (void)senderSMS {
    if (self.phoneTextField.text.length && self.codeTextField.text.length) {
        [HYNetHelper verifyMobile:self.phoneTextField.text countryCode:self.codeTextField.text entity:@"User" callBack:^(JFResultVo *resultVo) {
            if (!resultVo.errorNo) {
                
            }
        }];
    }
}

- (void)requsetUsers {
    WEAKSELF
    [HYNetHelper allUsers:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo) {
            weakSelf.codeTextField.text = [NSString stringWithFormat:@"%@", resultVo.results[@"country_code"]];
            weakSelf.phoneTextField.text = [NSString stringWithFormat:@"%@", resultVo.results[@"mobile"]];
        }
    }];
}

#pragma mark - Table view data source
- (void)onValueDidChange:(UITextField *)textField {
    if ((self.codeTextField.text.length > 0) && (self.phoneTextField.text.length > 0) && (self.SMSTextField.text.length > 0)) {
        self.nextButton.enabled = YES;
    }else {
        self.nextButton.enabled = NO;
    }
}

- (IBAction)nextAction:(id)sender {
    AccountDepositNextViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"AccountDepositNextViewController"];
    vc.balanceEntity = self.balance;
    vc.wallet_id = self.wallet_id;
    vc.type = self.type;
    vc.title = self.title;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)selectCountryCode:(id)sender {
//    NSArray *array = @[@"852", @"86", @"853"];
//    SheetView *view = [[[NSBundle mainBundle] loadNibNamed:@"SheetView" owner:nil options:nil] firstObject];
//    view.frame = self.navigationController.view.bounds;
//    view.titleArray = array;
    WEAKSELF
//    view.selectAction = ^(NSInteger index) {
//        weakSelf.codeTextField.text = array[index];
//    };
//    [self.navigationController.view addSubview:view];
    JFAreaCodeTableViewController *vc = [[JFAreaCodeTableViewController alloc]init];
    vc.returnCountryCodeBlock = ^(NSString *areaCodeStr) {
        weakSelf.codeTextField.text = areaCodeStr;
    };
    [self.navigationController pushViewController:vc animated:YES];
}


@end
